package org.m8.auth.model;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.QBean;
import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.dml.SQLUpdateClause;
import org.m8.auth.model.qentity.QSocialUserBind;
import org.m8.dba.config.SQLQueryFactoryManager;
import org.m8.dba.platform.model.builder.UpdateBuilder;
import org.springframework.stereotype.Service;

/**
 * 社交用户绑定dao
 *
 * @author yidanjun
 * @date 2024/9/5
 **/
@Service
public class SocialUserBindDao {
    public final QBean<SocialUserBindDO> qFileBean = Projections.bean(SocialUserBindDO.class, QSocialUserBind.m8SocialUserBind.all());

    /**
     * 新增社交用户绑定
     *
     * @param socialUserBindDO 社交用户
     * @return
     */
    public long inert(SocialUserBindDO socialUserBindDO) {
        SQLQueryFactory sqlQueryFactory = SQLQueryFactoryManager.getQueryFactory();
        return sqlQueryFactory.insert(QSocialUserBind.m8SocialUserBind)
                .populate(socialUserBindDO)
                .execute();
    }

    /**
     * 根据社交类型和openId查询
     *
     * @param socialType    社交类型
     * @param openId        openId
     * @return SocialUserBindDO
     */
    public SocialUserBindDO queryBySocialTypeAndOpenId(String socialType,String openId) {
            SQLQueryFactory sqlQueryFactory = SQLQueryFactoryManager.getQueryFactory();
            return sqlQueryFactory.select(qFileBean)
                    .from(QSocialUserBind.m8SocialUserBind)
                    .where(QSocialUserBind.m8SocialUserBind.openid.eq(openId)
                            .and(QSocialUserBind.m8SocialUserBind.socialType.eq(socialType)))
                    .fetchOne();
        }


    public SocialUserBindDO selectByTypeAndCodeAnState(String socialType,String code, String state) {
        SQLQueryFactory sqlQueryFactory = SQLQueryFactoryManager.getQueryFactory();
        return sqlQueryFactory.select(qFileBean)
                .from(QSocialUserBind.m8SocialUserBind)
                .where(QSocialUserBind.m8SocialUserBind.code.eq(code)
                        .and(QSocialUserBind.m8SocialUserBind.socialType.eq(socialType))
                        .and(QSocialUserBind.m8SocialUserBind.state.eq(state)))
                .fetchOne();
    }

    /**
     * 根据id更新
     */
    public long updateById(SocialUserBindDO socialUserBindDO) {
        SQLQueryFactory sqlQueryFactory = SQLQueryFactoryManager.getQueryFactory();
        SQLUpdateClause sqlUpdateClause = sqlQueryFactory.update(QSocialUserBind.m8SocialUserBind);
        UpdateBuilder.build(sqlUpdateClause)
                .setIfPresent(QSocialUserBind.m8SocialUserBind.updateTime, socialUserBindDO.getUpdateTime())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.updater, socialUserBindDO.getUpdater())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.userId, socialUserBindDO.getUserId())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.state, socialUserBindDO.getState())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.code, socialUserBindDO.getCode())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.openid, socialUserBindDO.getOpenid())
                .setIfPresent(QSocialUserBind.m8SocialUserBind.socialType, socialUserBindDO.getSocialType());

        sqlUpdateClause.where(QSocialUserBind.m8SocialUserBind.id.eq(socialUserBindDO.getId()));
        return sqlUpdateClause.execute();

    }

    /**
     * 根据用户id和社交类型删除
     *
     * @param userId 用户id
     * @param socialType 社交类型
     * @return
     */
    public long deleteByUerIdAndSocialType(String userId, String socialType) {
        SQLQueryFactory sqlQueryFactory = SQLQueryFactoryManager.getQueryFactory();
        return sqlQueryFactory.delete(QSocialUserBind.m8SocialUserBind)
                .where(QSocialUserBind.m8SocialUserBind.userId.eq(userId)
                        .and(QSocialUserBind.m8SocialUserBind.socialType.eq(socialType)))
                .execute();
    }

}
